<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-6-3
 * Func   提现
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('user_money_cash');
$utable = Table('user');

if(IS_POST)
{
	$id = intval($_POST['id']);
	!$id && AjaxReturn(1,'提现信息不存在！');
	
	$data = $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id);
	!$data && AjaxReturn(1,'提现信息不存在！');
	$data['status'] && AjaxReturn(1,'该提现已经审核！');
    
	$status = $_POST['status'] == 1 ? 1 : 2;
	$notes = NoBadStr($_POST['notes']);

	// 提现成功做money_log日志操作
	if($status == 1){
		$tmp = [];
		$tmp['uid']		=	$data['uid'];
		$tmp['balance']	=	-$data['money'];
		$tmp['tid']		=	'cash';
		$tmp['lkey']	=	'cash_'.$data['orderid'];
		$tmp['money']	=	$data['money'];
		$tmp['notes']	=	'提现成功：'.$notes;
		$res = MoneyLog($tmp);
		!$res && AjaxReturn(1,'操作失败，请重新操作');
	}
	
	// //	拒绝会退款到账号
	// if($status == 2)
	// {
	// 	$tmp = [];
	// 	$tmp['uid']		=	$data['uid'];
	// 	$tmp['balance']	=	$data['money'];
	// 	$tmp['tid']		=	'cash';
	// 	$tmp['lkey']	=	'cash_fail_'.$_uid.'_'.$data['uid'].'_'.$id;
	// 	$tmp['money']	=	$data['money'];
	// 	$tmp['notes']	=	'提现失败：'.$notes;
	// 	$res = MoneyLog($tmp);
	// 	!$res && AjaxReturn(1,'操作失败，请重新操作');
	// }
	
	$db->exec('UPDATE '.$table.' SET notes = \''.$notes.'\',dotime = '.TIMESTAMP.',status = '.$status.' WHERE id = '.$id);
	ShowMsg('操作成功！',U('user/cash'));
	// AjaxReturn(0,'操作成功！');
}

if($a == 'add')
{
	$id = intval($_GET['id']);
	!$id && ShowMsg('提现信息不存在！','?m='.$m.'&c='.$c);
	
	$data = $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id);
	!$data && ShowMsg('提现信息不存在！','?m='.$m.'&c='.$c);
	$data['status'] && ShowMsg('该提现已经审核！','?m='.$m.'&c='.$c);
	
    $tpl->display('user/cash_add');
}
else
{
	$statuss = ['待审核','提现成功','提现失败'];
	$data  = array();
	$where = ' FROM '.$table.' t LEFT JOIN '.$utable.' u ON t.uid = u.id WHERE 1 ';
    
    $uid = intval($_GET['uid']);
	$uid && $where .= ' AND t.uid = '.$uid;
    
    $status = intval($_GET['status']);
	$status && $where .= ' AND t.status = '.($status > 0 ? $status : 0);
    
	$title = NoBadStr($_GET['title']);
	$title && $where .= ' AND (t.bankcard LIKE \'%'.$title.'%\' OR t.truename LIKE \'%'.$title.'%\' OR t.notes LIKE \'%'.$title.'%\' OR u.username LIKE \''.$title.'\') ';

	$tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
	if($tmp['p'])
	{
		$page = max(1,intval($_GET['page']));
		$size = 20;
		$limit    = ' LIMIT '.($page - 1) * $size.','.$size;
		
		$orderby = ' ORDER BY t.id DESC ';
		$url = '?m='.$m.'&c='.$c.'&a='.$a.'&title='.$title.'&status='.$status.'&uid='.$uid;
		
		$data = $db->getall('SELECT t.*,u.username '.$where.$orderby.$limit);
		$lastdata = current($data);

		$multi = Multi($url,$page,$tmp['p'],$size);
	}
	
	$tpl->display('user/cash');
}